package date;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import validate.ValidateUtils;

/**
 * 日期处理工具.
 *
 * @author guanweichen
 * @version 2010-9-17
 */
public class DateUtil {

    private static final long   ONE_DAY        = 86400000L;
    private static final String DEFAULT_FORMAT = "yyyy-MM-dd";

	/**
	 * 把日期转换成字符串,缺省日期转换
	 * 
	 * @param d
	 * @return String
	 */
	public static String defaultFormat(Date d) {
		return format(d, DEFAULT_FORMAT);
	}
	
	/**
	 * 把字符串转换成日期,缺省日期转换
	 * 
	 * @param s
	 * @throws WebException
	 * @return Date
	 */
	public static Date defaultParse(String s) {
		return parse(s, DEFAULT_FORMAT);
	}

	/**
	 * 日期按照指定格式转换.
	 * 
	 * @param d
	 * @param format
	 * @return String
	 */
	public static String format(Date d, String format) {
		if (d == null)
			return null;
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(d);
	}

	/**
	 * 字符串日期按照指定格式转换
	 * 
	 * @param s
	 * @param format
	 * @return Date
	 */
	public static Date parse(String s, String format) {

		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			return dateFormat.parse(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 得到日期与现在相距天数
	 * 
	 * @param df
	 * @return int
	 */
	public static int getDays(Date df) {
		
		return getDisDays(df, new Date());
	}

	/**
	 * 得到两个日期间相隔天数
	 * 
	 * @param df1
	 * @param df2
	 * @return int
	 */
	public static int getDisDays(Date df1, Date df2) {
		
		String a = String.valueOf(df1.getTime() - df2.getTime());
		String b = String.valueOf(ONE_DAY);
		BigDecimal dividend = new BigDecimal(a.toString());
		BigDecimal divisor = new BigDecimal(b.toString());
		return dividend.divide(divisor, 0, BigDecimal.ROUND_HALF_DOWN).intValue();
	}

	/**
	 * 日期小于当前日期，返回true
	 * 
	 * @param df
	 * @return boolean
	 */
	public static boolean isBeforeCurTime(Date df) {
		
		if (new Date().compareTo(df) > 0)
			return true;
		else
			return false;
	}


	/**
	 * 通过string得到Calendar.
	 * 
	 * @param str
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Calendar getCalendar(String str, String pattern) throws ParseException {
		
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = format.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * 返回当前时间月最大日期.
	 * 
	 * @param str
	 * @return
	 * @throws ParseException
	 */
	public static Date getActualMaximumDate(String str) throws ParseException {
		
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_FORMAT);
		Calendar calendar = getCalendar(str, DEFAULT_FORMAT);
		return format.parse(str.substring(0, str.lastIndexOf("-")) + "-" + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	}

	/**
	 * 返回当前时间月最小日期.
	 * 
	 * @param str
	 * @return
	 * @throws ParseException
	 */
	public static Date getActualMinimumDate(String str) throws ParseException {
		
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_FORMAT);
		Calendar calendar = getCalendar(str, DEFAULT_FORMAT);
		return format.parse(str.substring(0, str.lastIndexOf("-")) + "-" + calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
	}
	
	/**
	 * 设置时间的时分秒.
	 * 
	 * @param hour
	 * @param min
	 * @param Sec
	 * @return
	 */
	public static Date setDateHourMinSec(Date date, Integer hour, Integer min, Integer sec) {
		
		if (ValidateUtils.isEmpty(date)) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (!ValidateUtils.isEmpty(hour))
			calendar.set(Calendar.HOUR, hour);
		if (!ValidateUtils.isEmpty(min))
			calendar.set(Calendar.MINUTE, min);
		if (!ValidateUtils.isEmpty(sec))
			calendar.set(Calendar.SECOND, sec);
		date = calendar.getTime();
		return date;
	}

}
